﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Media;
using DiagramCreator.Nodes;
using System.Windows.Controls;
using System.Windows;

namespace DiagramCreator.Arrows
{
    public abstract class GenericConnectionLine : Shape, IDisposable
    {
        protected abstract void InternalDrawArrowGeometry(StreamGeometryContext context);

        private double x1;
        public double X1
        {
            get { return x1; }
            set { x1 = value; }
        }

        private double y1;
        public double Y1
        {
            get { return y1; }
            set { y1 = value; }
        }

        private double x2;
        public double X2
        {
            get { return x2; }
            set { x2 = value; }
        }

        private double y2;
        public double Y2
        {
            get { return y2; }
            set { y2 = value; }
        }

        #region - DefiningGeometry -

        protected override Geometry DefiningGeometry
        {
            get
            {
                // Create a StreamGeometry for describing the shape
                StreamGeometry geometry = new StreamGeometry();
                geometry.FillRule = FillRule.EvenOdd;

                using (StreamGeometryContext context = geometry.Open())
                {
                    InternalDrawArrowGeometry(context);
                }

                // Freeze the geometry for performance benefits
                geometry.Freeze();

                return geometry;
            }
        }

        #endregion

        #region - Parent i Child properies -

        private GenericNode parent;
        public GenericNode Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;
            }
        }

        private GenericNode child;
        public GenericNode Child
        {
            get
            {
                return child;
            }
            set
            {
                child = value;
            }
        }

        #endregion

        #region - Konstruktor -

        public GenericConnectionLine(GenericNode p, GenericNode ch)
        {
            parent = p;
            child = ch;            
        }

        #endregion

        #region - updatePosition -

        /// <summary>
        /// Uaktualnia wielkosc i polozenie
        /// na podstawie polozenia parent i childa
        /// </summary>
        public virtual void updatePosition()
        {
            double parentLeft = (double)parent.GetValue(Canvas.LeftProperty);
            double parentTop = (double)parent.GetValue(Canvas.TopProperty);

            this.X1 = Parent.GetCrossingPoint(new Point(child.MiddleX, child.MiddleY)).X;
            this.Y1 = Parent.GetCrossingPoint(new Point(child.MiddleX, child.MiddleY)).Y;

            Point p = new Point(X1, Y1);
            this.X2 = child.GetCrossingPoint(p).X;
            this.Y2 = child.GetCrossingPoint(p).Y;

            this.InvalidateVisual();
        }

        #endregion

        #region IDisposable Members

        public virtual void Dispose()
        {
            
        }

        #endregion
    }
}
